var regform = null;
var nickName = null;
var loginName = null;
var password = null;
var repassword = null;
var image = null;
var regBtn = null;
var backBtn = null;

$(function () {
    regform = $("#regForm");
    nickName = $("#userNickname");
    loginName = $("#userLoginname");
    password = $("#userPassword");
    repassword = $("#reuserPassword");
    image = $("#userImage");
    regBtn = $("#regButton");
    backBtn = $("#backButton");

    /**
     * 验证
     *失去焦点的时候
     * jquery的ajax方法异步交互
     */
    loginName.on("focus", function () {
        validateTip(loginName.next(), {"color": "#666666"}, "用户名是您登录平台的账号", false);
    }).on("blur", function () {
        $.ajax({
            type: "GET",//
            url: path + "/index/checkUserCode",
            data: {loginName: loginName.val()},
            dataType: "json",
            success: function (data) {
                if (data.message === "exist") {
                    validateTip(loginName.next(), {"color": "red"}, imageNo + " 该用户名已存在", false);
                } else if (data.message === "isNull"){
                    validateTip(loginName.next(), {"color": "red"}, imageNo + " 用户名不能为空", false);
                } else {
                    validateTip(loginName.next(), {"color": "green"}, imageYes, true);
                }
            },
            error: function (data) {
                validateTip(loginName.next(), {"color": "red"}, imageNo + " 您异步交互出问题啦", false);
            }
        });
    });


    nickName.on("focus", function () {
        //有友情提示连接
        validateTip(nickName.next(), {"color": "#666666"}, " 用户昵称长度必须大于1小于10", false);
    }).on("blur", function () {
        if (nickName.val() != null && nickName.val().length > 1 && nickName.val().length < 10) {
            validateTip(nickName.next(), {"color": "green"}, imageYes, true);
        } else {
            validateTip(nickName.next(), {"color": "red"}, imageNo + " 用户昵称不符合规范，请重新输入", false);
        }
    });

    password.on("focus", function () {
        //友情提示
        validateTip(password.next(), {"color": "#666666"}, " 密码长度必须是大于6小于20", false);
    }).on("blur", function () {
        if (password.val() != null && password.val().length > 6 && password.val().length < 20) {
            validateTip(password.next(), {"color": "green"}, imageYes, true);
        } else {
            validateTip(password.next(), {"color": "red"}, imageNo + " 密码输入不符合规范，请重新输入", false);
        }
    });

    repassword.on("focus", function () {
        validateTip(repassword.next(), {"color": "#666666"}, " 请再次确认密码", false);
    }).on("blur", function () {
        if (repassword.val() != null && repassword.val().length > 1 && repassword.val().length < 20
            && repassword.val() === password.val() ) {
            validateTip(repassword.next(), {"color": "green"}, imageYes, true);
        } else {
            validateTip(repassword.next(), {"color": "red"}, imageNo + " 两次密码输入不一致，请重新输入！", false);
        }
    });

    regBtn.on("click", function () {
        if (nickName.attr("validateStatus")!="true") {
            nickName.blur();
        } else if (loginName.attr("validateStatus")!="true") {
            loginName.blur();
        } else if (password.attr("validateStatus")!="true") {
            password.blur();
        } else if (repassword.attr("validateStatus")!="true") {
            repassword.blur();
        } else {
            if (confirm("确认信息无误了吗?")) {
                $("#regForm").submit();
            }
        }
    });


    backBtn.on("click", function () {
        if (referer != undefined
            && null != referer
            && "" != referer
            && "null" != referer
            && referer.length > 4) {
            window.location.href = referer;
        } else {
            history.back(-1);
        }
    });

})